// Domien Nowicki
#include "QtLaserSprite.h"
#include "QtBlendToEffect.h"

QtLaserSprite::QtLaserSprite(QtSprite *parent): QtPixmapSprite(parent), duration(1000), cellWidth(0), cellHeight(0)
{
	SetDepth(1);
	laserBeam = new QtLineSprite();
	laserBeam->SetColor(Qt::red);
	laserBeam->SetAlpha(0);
	laserBeam->SetLineWidth(3);
	laserBeam->SetDepth(10);

	AddSprite(laserBeam);

	SetResource("foreground", "laser");
}

QtLaserSprite::~QtLaserSprite()
{
	if (laserBeam)
	{
		delete laserBeam;
		laserBeam = 0;
	}
}

void QtLaserSprite::SetLaserColor(const QColor& color)
{
	laserBeam->SetColor(color);
}

void QtLaserSprite::SetDirection(const Direction& direction)
{
	switch(direction.Type())
	{
		case Direction::NORTH:
			SetAngle(0);
		break;

		case Direction::SOUTH:
			SetAngle(180);
		break;
	
		case Direction::WEST:
			SetAngle(270);
		break;
	
		case Direction::EAST:
			SetAngle(90);
		break;
	}
}


void QtLaserSprite::Shoot()
{
	QPointF beginPoint = MapToScene(QPointF(cellWidth/2, Height()/2));
	laserBeam->SetLine(beginPoint, laserDestination);

	QtBlendToEffect effect;

	effect.SetDuration(100);
	effect.SetTarget(laserBeam);
	effect.SetEndAlpha(1);

	AddEffect(&effect);

	effect.SetDuration(100);
	effect.SetTarget(laserBeam);
	effect.SetEndAlpha(0);

	AddEffect(&effect);

	Start();
}


void QtLaserSprite::SetLaserDestination(unsigned int targetCellX, unsigned int targetCellY)
{
	laserDestination = QPointF(targetCellX*cellWidth, targetCellY*cellHeight);
}

void QtLaserSprite::SetCellSize(unsigned int width, unsigned int height)
{
	SetCenterPoint(QPointF(width/2, height/2));
	cellWidth = width;
	cellHeight = height;
}



